using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
using Entity; 

namespace Business
{
    public class bPlan
    {
        public static int insertPlan(ePlan plan)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@planName", SqlDbType.NVarChar);
            pr.Value = plan.planName;
            paramList.Add(pr);

            pr = new SqlParameter("@planDescription", SqlDbType.NVarChar);
            pr.Value = plan.planDescription;
            paramList.Add(pr);

            pr = new SqlParameter("@productID", SqlDbType.Int);
            pr.Value = plan.productID;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("insertPlan", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static int updatePlan(ePlan plan)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@planID", SqlDbType.Int);
            pr.Value = plan.planID;
            paramList.Add(pr);

            pr = new SqlParameter("@planName", SqlDbType.NVarChar);
            pr.Value = plan.planName;
            paramList.Add(pr);

            pr = new SqlParameter("@planDescription", SqlDbType.NVarChar);
            pr.Value = plan.planDescription;
            paramList.Add(pr);

            pr = new SqlParameter("@productID", SqlDbType.Int);
            pr.Value = plan.productID;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("updatePlan", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static int deletePlan(int planID)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@planID", SqlDbType.Int);
            pr.Value = planID;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("deletePlan", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static DataTable getAllPlan()
        {
            DBHelper dbh = new DBHelper();
            DataTable dt = dbh.ExecuteQueryDataTable("getAllPlan", CommandType.StoredProcedure, new List<SqlParameter>());
            dbh.CloseConnection();
            return dt;
        }

        public static DataTable getPlanByProductID(int ProductID)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();

            SqlParameter pr = new SqlParameter("@productID", SqlDbType.Int);
            pr.Value = ProductID;
            paramList.Add(pr);

            DataTable dt = dbh.ExecuteQueryDataTable("getPlanByProductID", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return dt;
        }
        public static ePlan getPlanById(int planID)
        {
            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@planID", SqlDbType.Int);
            pr.Value = planID;
            paramList.Add(pr);

            IList il = bBase.LoadQueryString(typeof(ePlan), "getPlanById", CommandType.StoredProcedure, paramList);
            if (il.Count > 0)
            {
                return (ePlan)il[0];
            }
            else
            {
                return null;
            }
        }

        public static ePlan getLastPlan()
        {
            IList<SqlParameter> paramList = new List<SqlParameter>();

            IList il = bBase.LoadQueryString(typeof(ePlan), "getLastPlan", CommandType.StoredProcedure, paramList);
            if (il.Count > 0)
            {
                return (ePlan)il[0];
            }
            else
            {
                return null;
            }
        }
    }
}
